package java44th.group.service.wx.comment;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketComment;
import java44th.group.bean.po.MarketCommentExample;
import java44th.group.bean.po.MarketUser;
import java44th.group.bean.wx.bo.commont.ListBo;
import java44th.group.bean.wx.bo.commont.PostBo;
import java44th.group.bean.wx.vo.comment.CountVo;
import java44th.group.bean.wx.vo.comment.ListVo;
import java44th.group.bean.wx.vo.comment.PostVo;
import java44th.group.bean.wx.vo.comment.UserInfoEntity;
import java44th.group.mapper.MarketCommentMapper;
import java44th.group.mapper.MarketUserMapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName project-second
 * @Descriptiom TODO
 * @Aurhor: 蔡文强
 * @Since: 2022/10/15 15:45
 */
@Service
public class WxCommentServiceImpl implements WxCommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public CommonData<ListVo> queryCommentList(ListBo listBo) {

        // 挑选出符合条件的图片信息并封装好分页信息
        PageHelper.startPage(listBo.getPage(), listBo.getLimit());

        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(listBo.getType().byteValue());
        // 没有值就是查询全部
        // 很奇怪这个地方
        if (listBo.getValueId() != null) {
            criteria.andValueIdEqualTo(listBo.getValueId());
        }
        // ShowType =1 表示需要返回带有图片的评论
        if (listBo.getShowType() == 1) {
            criteria.andHasPictureEqualTo(true);
        }
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(example);

        PageInfo<MarketComment> marketCommentPageInfo = new PageInfo<>(marketComments);

        // 最终需要返回的数组
        ArrayList<ListVo> listVos = new ArrayList<>();

        // 封装各个参数
        for (MarketComment marketComment : marketComments) {
            ListVo listVo = new ListVo();
            listVo.setAddTime(marketComment.getAddTime());
            listVo.setPicList(marketComment.getPicUrls());
            listVo.setAdminContent(marketComment.getAdminContent());
            listVo.setContent(marketComment.getContent());

            MarketUser marketUser = marketUserMapper.selectByPrimaryKey(marketComment.getUserId());
            // 这里要返回NickName的我改成username了，因为没多少人在数据库里会写nickname --
            UserInfoEntity userInfoEntity = new UserInfoEntity(marketUser.getAvatar(), marketUser.getUsername());
            listVo.setUserInfo(userInfoEntity);

            listVos.add(listVo);
        }

        return CommonData.dataWithList(marketCommentPageInfo, listVos);
    }


    @Override
    public CountVo queryCommentCount(Integer valueId, Integer type) {

        // 收集所有评论
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(type.byteValue());
        // 同上，很奇怪
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        long allCount = marketCommentMapper.countByExample(example);

        // 只收集带有图片的图片
        MarketCommentExample exampleHasPic = new MarketCommentExample();
        MarketCommentExample.Criteria criteriaHasPic = exampleHasPic.createCriteria();
        criteriaHasPic.andHasPictureEqualTo(true);
        criteriaHasPic.andTypeEqualTo(type.byteValue());
        // 同上，很奇怪
        if (valueId != null) {
            criteriaHasPic.andValueIdEqualTo(valueId);
        }
        long hasPicCount = marketCommentMapper.countByExample(exampleHasPic);

        CountVo countVo = new CountVo(new Long(allCount).intValue(), new Long(hasPicCount).intValue());

        return countVo;
    }

    @Override
    public PostVo insertCommentAndReturnInfo(PostBo postBo, Integer userId) {


        MarketComment marketComment = new MarketComment();
        marketComment.setHasPicture(postBo.isHasPicture());
        marketComment.setValueId(postBo.getValueId());
        marketComment.setStar(new Integer(postBo.getStar()).shortValue());
        marketComment.setType(new Integer(postBo.getType()).byteValue());
        marketComment.setPicUrls(postBo.getPicUrls());
        marketComment.setContent(postBo.getContent());
        marketComment.setUserId(userId);

        marketCommentMapper.insertNewComment(marketComment);

        MarketComment newComment = marketCommentMapper.selectByPrimaryKey(marketComment.getId());
        PostVo postVo = new PostVo(newComment.getHasPicture(), newComment.getValueId(), newComment.getStar(),
                newComment.getAddTime(), newComment.getUpdateTime(), newComment.getId(),
                newComment.getType(), newComment.getPicUrls(), newComment.getUserId(),
                newComment.getContent());

        return postVo;
    }
}
